var express = require('express');
var router = express.Router();
var db = require('../sql.js');

/* GET users listing. */
router.get('/', function(req, res, next) {
  var pageNum = req.query.page;
  
  db.query('select * from banner',function(err,result){
    var pager = {};
    // 当前页码
    pager.pageCurrent = pageNum || 1;
    // 总条数
    pager.maxNum = result.length;
    // 每页条数
    pager.pageSize = 5;
    // 总页数
    pager.pageCount = parseInt(Math.ceil(pager.maxNum / pager.pageSize));
    // 页码列表
    var dataList = result.slice((pager.pageCurrent - 1) * pager.pageSize, (pager.pageCurrent - 1) * pager.pageSize + pager.pageSize);

    if(err){
      console.log(err);
      return;
    }
    // console.log(result);
    res.render('bannerList',{
      bannerList:dataList,
      pager:pager
    })

  })
});

module.exports = router;
